import * as util from './util'

export class IGitSourceParams {
    /**
     * The source index
     */
    order!: number
    /**
     * The process work space
     */
    workDir!: string

    /**
     * The repository type
     */
    type!: string

    /**
     * The location where the repository will be download
     */
    projectDir!: string

    /**
     * The repository name
     */
    name!: string

    /**
     * The host of jenkins
     */
    host: string | undefined

    /**
     * The job path of jenkins
     */
    jobPath: string | undefined

    /**
     * The host of the repository to be cloned
     */
    gitRepo!: string

    /**
     * Could be svn repo as well
     */
    repo!: string

    /**
     * Code branch
     */
    branch: string | undefined

    /**
     * Indicates whether to checkout submodules when fetching
     */
    submodule!: boolean

    /**
     * The depth when fetching
     */
    depth!: number

    /**
     * Indicates whether to open branch mode when fetching
     */
    branchMode!: boolean

    /**
     * The code latest submit id
     */
    commitId: string | undefined

    /**
     * The code previous submit id
     */
    previousCommitId: string | undefined

    /**
     * The username to use when fetching the repository
     */
    user: string | undefined

    /**
     * The password to use when fetching the repository
     */
    password: string | undefined

    /**
     * The policy(http/ssh) for fetching the repository
     */
    hostGitMethod!: string

    /**
     * Http policy
     */
    httpPolicy!: string

    /**
     * Indicates whether to use Http policy
     */
    httpUrlWithCredential!: boolean

    /**
     * Indicates whether to use SSH policy
     */
    sshUrl: boolean | undefined

    /**
     * The SSH key to configure when hostGitMethod is SSH
     */
    hostGitPK: string | undefined

    /**
     * The repository url
     */
    cloneUrl!: string

    /**
     * The repository endpoint
     */
    gitEndpoint!: string

    /**
     * STS token when source type is flow
     */
    token: string | undefined

    /**
     * Packages info when type is flow
     */
    packages: any | undefined

    /**
     * packages info 
     */
    repoType!: string

    artifactName!: string

    versionName!: string

    repoId!: string

    orgId!: string 

    repoUrl!: string 

    packagesDownloadUrl!: string

    packagesFilePath?: string


    svnCheckoutDir(): string {
        return (this.branch?.includes('/') ? this.branch.split('/')[1] : this.branch)!
    }

    svnCheckoutDirFull(): string {
        return this.projectDir + util.SEP + this.svnCheckoutDir()
    }

    svnCheckoutUrl() {
        if(this.repo.endsWith('/')) {
            return `${this.repo}${this.branch}`
        }
        return `${this.repo}/${this.branch}`
    }

    svnRepoName(): string {
        if(this.repo.endsWith('/')) {
            return this.repo.split('/').slice(-2, -1)[0]
        }
        return this.repo.split('/').pop()!
    }
}
